/*
 * NativeTest.h  0.4.0 / Nov 14, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#ifndef NATIVETEST_H
#define NATIVETEST_H

#ifdef CUTE4J_TEST

#include "Cute4jCore.h"

// NativeTest Class. -----------------------------------------------------------

/*!
 * \brief Native Test.
 *
 * \author Slobodan Vrkacevic
 * \version 0.4.0
 */
class CUTE4J_EXPORT NativeTest : public QObject
{
  Q_OBJECT
public:
  jobjectArray getTestMethods(JNIEnv *env);
  void setUp(JNIEnv *env);
  void tearDown(JNIEnv *env);
  bool invokeMethod(JNIEnv *env, jobject obj, jstring methodName);
public:
  static void initialize(JNIEnv *env, jclass cls);
protected:
  virtual void setUp();
  virtual void tearDown();
protected:
  jobject createJObject(const char *className);
  jobject createJObject(const char *className, const char *ctorSig, ...);
  void *nativeObjectPointer(jobject obj);
  void *nativeObjectPointer(jobject obj, const char *fieldName);
  template<typename T> T nativePointer(jobject obj) {
    return reinterpret_cast<T>(nativeObjectPointer(obj));
  }
  template<typename T> T nativePointer(jobject obj, const char *fieldName) {
    return reinterpret_cast<T>(nativeObjectPointer(obj, fieldName));
  }
  void failCondition(const char *condition = 0, const char *fileName = 0,
                     int lineNumber = -1);
protected:
  inline JNIEnv *env() const;
private:
  JNIEnv *m_env;
  jweak m_obj;
private:
  static jmethodID sm_failConditionMID;
};

// NativeTest :: Protected Inline Member Function. -----------------------------

/*!
 * \brief JNIEnv pointer.
 *
 * \return JNIEnv pointer
 */
inline JNIEnv *NativeTest::env() const
{
  return m_env;
}

// Macro Definitions. ----------------------------------------------------------

#define nativeTest (reinterpret_cast<NativeTest *>(testCase))

#endif // CUTE4J_TEST

#endif // NATIVETEST_H
